home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / ACORNUSERS / EMULATOR / MAGICKIT / assembler / c / symbol < prev   
Text File  |  1998-04-14  |  3KB  |  178 lines

  1. #include <stdio.h>
  2. #include "defs.h"
  3. #include "externs.h"
  4.  
  5. /* colsym() collects a symbol from prlnbuf into symbol[],
  6.  *    leaves prlnbuf pointer at first invalid symbol character,
  7.  *    returns 0 if no symbol collected
  8.  */
  9.  
  10. int colsym(int *ip)
  11. {
  12.     int    valid;
  13.     int    i;
  14.     char c;
  15.  
  16.     valid = 1;
  17.     i = 0;
  18.     while (valid == 1) {
  19.         c = prlnbuf[*ip];
  20.         if (c == '_' || c == '.');
  21.         else if (c >= 'a' && c <= 'z');
  22.         else if (c >= 'A' && c <= 'Z');
  23.         else if (i >= 1 && c >= '0' && c <= '9');
  24.         else valid = 0;
  25.         if (valid == 1) {
  26.             if (i < SBOLSZ - 1)
  27.                 symbol[++i] = c;
  28.             (*ip)++;
  29.         }
  30.     }
  31.     if (i == 1) {
  32.         switch (symbol[1]) {
  33.         case 'A': case 'a':
  34.         case 'X': case 'x':
  35.         case 'Y': case 'y':
  36.             error("Symbol is reserved (A, X or Y)!");
  37.             i = 0;
  38.         }
  39.     }
  40.     symbol[0] = i;
  41.     symbol[i+1] = '\0';
  42.     return(i);
  43. }
  44.  
  45. /* symbol table lookup
  46.  *    if found, return pointer to symbol
  47.  *    else, install symbol as undefined, and return pointer
  48.  */
  49.  
  50. struct t_symbol *stlook(void)
  51. {
  52.     struct t_symbol *ptr;
  53.     unsigned int hash = 0;
  54.     char c;
  55.     int    i;
  56.  
  57.     if (symbol[1] == '.'){        /*  Local symbol */
  58.         if (glablptr) {
  59.             ptr = glablptr->local;
  60.             while (ptr) {
  61.                 if (!strcmp(symbol, ptr->name))
  62.                     break;
  63.                 ptr = ptr->next;
  64.             }
  65.             if (ptr == NULL)
  66.                 ptr = stinstal(hash, 1);
  67.         } else {
  68.             error("Local symbol not allowed!");
  69.             return (NULL);
  70.         }
  71.     } else {                    /*  Global symbol */
  72.         for (i = 0; i < symbol[0]; i++) {
  73.             c = symbol[i + 1];
  74.             hash += c;
  75.             hash  = (hash << 3) + (hash >> 5) + c;
  76.         }
  77.         hash &= 0xFF;
  78.         ptr = hash_tbl[hash];
  79.         while (ptr) {
  80.             if (!strcmp(symbol, ptr->name))
  81.                 break;
  82.             ptr = ptr->next;
  83.         }
  84.         if (ptr == NULL)
  85.             ptr = stinstal(hash, 0);
  86.     }
  87.     return(ptr);
  88. }
  89.  
  90. /* install symbol into symtab */
  91.  
  92. struct t_symbol *stinstal(int hash, int type)
  93. {
  94.     struct t_symbol *ptr;
  95.  
  96.     if ((ptr = (void *)malloc(sizeof(struct t_symbol))) == NULL) {
  97.         error("Out of memory!");
  98.         return (NULL);
  99.     }
  100.     ptr->type  = UNDEF;
  101.     ptr->value = 0;
  102.     ptr->local = NULL;
  103.     ptr->bank  = -1;
  104.     ptr->page  = -1;
  105.     strcpy(ptr->name, symbol);
  106.  
  107.     if (type >= 1) {
  108.         ptr->next = glablptr->local;
  109.         glablptr->local = ptr;
  110.     } else {
  111.         ptr->next = hash_tbl[hash];
  112.         hash_tbl[hash] = ptr;
  113.     }
  114.     return(ptr);
  115. }
  116.  
  117. /* remove the latest defined symbol from symtab */
  118.  
  119. int stremove(void)
  120. {
  121.     char c;
  122.     int i, hash = 0;
  123.  
  124.     for (i = 0; i < symbol[0]; i++) {
  125.         c = symbol[i + 1];
  126.         hash += c;
  127.         hash  = (hash << 3) + (hash >> 5) + c;
  128.     }
  129.     hash &= 0xFF;
  130.     if (hash_tbl[hash] != lablptr) {
  131.         error("Internal error[2]!");
  132.         return (0);
  133.     }
  134.     hash_tbl[hash] = lablptr->next;
  135.     free(lablptr);
  136.     return (1);
  137. }
  138.  
  139. /* assign <value> to label pointed to by lablptr,
  140.  *    checking for valid definition, etc.
  141.  */
  142.  
  143. int labldef(int lval, int flag)
  144. {
  145.     char c;
  146.     int    i;
  147.  
  148.     if (lablptr) {
  149.         if (flag)
  150.             lval = (lval & 0x1FFF) | (page << 13);
  151.         if (pass == FIRST_PASS) {
  152.             if (lablptr->type == UNDEF) {
  153.                 lablptr->type = DEFABS;
  154.                 lablptr->value = lval;
  155.             } else {
  156.                 lablptr->type = MDEF;
  157.                 lablptr->value = 0;
  158.                 error("Label multiply defined!");
  159.                 return(-1);
  160.             }
  161.         } else {
  162.             if ((lablptr->value != lval) && (pass == LAST_PASS)) {
  163.                 error("Internal error[1]!");
  164.                 return(-1);
  165.             }
  166.         }
  167.         if (flag) {
  168.             lablptr->bank = bank;
  169.             lablptr->page = page;
  170.             c = lablptr->name[1];
  171.             if (c != '.')
  172.                 glablptr = lablptr;
  173.         }
  174.     }
  175.     return(0);
  176. }
  177.  
  178.